package cn.HuanG.controller;

import cn.HuanG.service.IShopService;
import cn.HuanG.domain.Shop;
import cn.HuanG.util.AjaxResult;
import cn.HuanG.util.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.updateById(shop);
            else
                shopService.insert(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody cn.HuanG.hrm.query.ShopQuery query)
    {
        Page<Shop> page = new Page<Shop>(query.getPage(),query.getRows());
//        page = shopService.selectPage(page);
        //拼接sql语句
        EntityWrapper<Shop> wrapper = new EntityWrapper<>();
        //select  * from t_emp where name  like concat(#{keyword},"%")  and admin_id =327;
        //具体拼接代码可参考官网代码解释
        wrapper.like("name", query.getKeyword()).and().eq("admin_id",327);
        shopService.selectPage(page,wrapper);
        return new PageList<Shop>(page.getTotal(),page.getRecords());
    }
}
